<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录界面</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      background-color: #f4f4f4;
    }
    .login-container {
      max-width: 400px;
      margin: 100px auto;
      padding: 20px;
      background-color: white;
      box-shadow: 0 0 10px rgba(0,0,0,0.1);
      text-align: center;
    }
    .login-container h2 {
      color: #333;
    }
    .login-form {
      margin-top: 20px;
    }
    .login-form input[type="text"],
    .login-form input[type="password"] {
      width: 100%;
      padding: 10px;
      margin-bottom: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
    .login-form button {
      width: 100%;
      padding: 10px;
      background-color: #4CAF50;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
    .login-form button:hover {
      background-color: #45a049;
    }
  </style>
</head>
<body>

<div class="login-container">
  <h2>登录</h2>
  <form class="login-form" id="login-form">
    <input type="text" id="username" placeholder="用户名" required>
    <input type="password" id="password" placeholder="密码" required>
    <button type="button" id="login-btn">登录</button>
    <button type="button" id="register-btn">注册</button> <!-- 新增注册按钮 -->
  </form>
</div>

<script>
  // 登录按钮点击事件
  document.getElementById("login-btn").addEventListener("click", function () {
    const username = document.getElementById("username").value;
    const password = document.getElementById("password").value;

    const loginData = { username, password };

    fetch('http://localhost:8080/users/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(loginData),
    })
      .then((response) => response.json())
      .then((data) => {
        if (data.token) {
          localStorage.setItem("jwtToken", data.token);
          window.location.href = "http://localhost:63342/MESSAGE/fronted/src/app.html";
        } else {
          alert(data.message || "登录失败！");
        }
      })
      .catch((error) => {
        console.error("Login error:", error);
        alert("登录失败，请重试！");
      });
  });

  // 注册按钮点击事件
  document.getElementById("register-btn").addEventListener("click", function () {
    const username = document.getElementById("username").value;
    const password = document.getElementById("password").value;

    const registerData = { username, password };

    fetch('http://localhost:8080/users/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(registerData),
    })
      .then((response) => response.json())
      .then((data) => {
        alert("注册成功！");
        // 注册成功后跳转到登录页面
      })
      .catch((error) => {
        console.error("Registration error:", error);
        alert("注册失败，请重试！");
      });
  });

  // 页面加载时检查是否已登录
  document.addEventListener("DOMContentLoaded", () => {
    const token = localStorage.getItem("jwtToken");
    if (token) {
      fetch('http://localhost:8080/users/validate', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${token}`,
        },
      })
        .then((response) => {
          if (response.status === 200) {
            window.location.href = "http://localhost:63342/MESSAGE/fronted/src/app.html";
          }
        })
        .catch((error) => {
          console.error("Token validation error:", error);
          localStorage.removeItem("jwtToken");
        });
    }
  });
</script>

</body>
</html>
